package com.infisight.hudprojector;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Fragment;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.PixelFormat;
import android.net.wifi.p2p.WifiP2pManager;
import android.net.wifi.p2p.WifiP2pManager.Channel;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.navi.AMapNavi;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.infisight.hudprojector.common.HudProjectApplication;
import com.infisight.hudprojector.data.MenuClass;
import com.infisight.hudprojector.data.MsgInfoClass;
import com.infisight.hudprojector.data.MusicTypeClass;
import com.infisight.hudprojector.data.SpeechVoiceData;
import com.infisight.hudprojector.data.StartNaviInfo;
import com.infisight.hudprojector.data.TelInfoClass;
import com.infisight.hudprojector.data.VoiceDataClass;
import com.infisight.hudprojector.fragment.AllPlanSeeFragment;
import com.infisight.hudprojector.fragment.GNaviFragment;
import com.infisight.hudprojector.fragment.HomePJFragment;
import com.infisight.hudprojector.fragment.HudFragment;
import com.infisight.hudprojector.fragment.MusicFragment;
import com.infisight.hudprojector.fragment.MusicPlayFragment;
import com.infisight.hudprojector.fragment.NewMsgFragment;
import com.infisight.hudprojector.fragment.NewPhoneFragment;
import com.infisight.hudprojector.fragment.OBDFragment;
import com.infisight.hudprojector.fragment.PhoneFragment;
import com.infisight.hudprojector.hotspot.WifiAdmin;
import com.infisight.hudprojector.hotspot.WifiApAdmin;
import com.infisight.hudprojector.kdxfspeech.SpeechVoiceCompound;
import com.infisight.hudprojector.util.CommonUtil;
import com.infisight.hudprojector.util.Constants;

/**
 * 进入HUD的首页 需要在首页进行百度导行引擎的初始化工作
 */
@SuppressLint("NewApi")
public class MainActivity extends Activity implements View.OnClickListener {
	String TAG = "MainActivity";
	// 与路径规划相关的内容
	WifiP2pManager manager;
	Channel channel;
	WifiApAdmin wifihot = null;
	boolean isWifiP2pEnabled = false;
	final IntentFilter intentFilter = new IntentFilter();
	List<MenuClass> lstMC = new ArrayList<MenuClass>();
	LinearLayout ll_menus;
	TextView main_tv_time, main_rui_state;
	int selectPosition = 0;
	HashMap<Integer, Integer> hmControlsPosition = new HashMap<Integer, Integer>();
	private boolean mIsEngineInitSuccess = false;
	// 显示
	// FrameLayout nav_container;
	FrameLayout info_container;
	int OperFragmentFlag = 2;// 如果在nav_container，则为0，如果在info_container,则为1，如果在菜单项，则为2；
								// 向左减，向右加，最小为0，最大为2
	// 想定popupwindow 相关的数据
	private PopupWindow popupWindow;
	private final Timer timer = new Timer();
	private DismissPopupWindowTask task;

	// 起点终点列表
	// private ArrayList<NaviLatLng> mStartPoints = new ArrayList<NaviLatLng>();
	// private ArrayList<NaviLatLng> mEndPoints = new ArrayList<NaviLatLng>();
	AMapNavi mAMapNavi;
	StartNaviInfo sni;
	RelativeLayout main_rl_1;
	SpeechVoiceCompound svc = null;
	TextView main_tv_back_music;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		svc = SpeechVoiceCompound.getInstance(MainActivity.this, "", null);// 语音合成
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		int height = this.getWindowManager().getDefaultDisplay().getHeight();
		getSharedPreferences("screen_height", 0).edit()
				.putInt("screen_height", height).commit();

		Log.i(TAG, height + "-----");
		requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置无标题
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_main);

		getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
				WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);// 设置不息屏
		InitView();
//		creatHotSpot();
		// 创建悬浮窗口
	}

	// 创建一个热点
	public void creatHotSpot() {
		WifiApAdmin wifiAp = new WifiApAdmin(MainActivity.this);
		WifiAdmin wifiA = new WifiAdmin(MainActivity.this) {

			@Override
			public void onNotifyWifiConnected() {
				// TODO Auto-generated method stub

			}

			@Override
			public void onNotifyWifiConnectFailed() {
				// TODO Auto-generated method stub

			}

			@Override
			public void myUnregisterReceiver(BroadcastReceiver receiver) {
				// TODO Auto-generated method stub

			}

			@Override
			public Intent myRegisterReceiver(BroadcastReceiver receiver,
					IntentFilter filter) {
				// TODO Auto-generated method stub
				return null;
			}
		};
		wifiAp.startWifiAp("HudProjectZYH", "66666666");
	}

	/**
	 * 初始化空间，这个还需要再考虑是不是要直接开起Wifi-Direct功能
	 */
	private void InitView() {

		mAMapNavi = AMapNavi.getInstance(MainActivity.this);
		main_tv_time = (TextView) findViewById(R.id.main_tv_time);
		main_tv_time.setText(CommonUtil.timeShow());
		main_rl_1 = (RelativeLayout) findViewById(R.id.main_rl_1);
		main_rui_state = (TextView) findViewById(R.id.main_rui_state);
		main_tv_back_music = (TextView) findViewById(R.id.main_tv_back_music);
		if (!CommonUtil.isGPSOpen(MainActivity.this)) {
			CommonUtil.openGPS(MainActivity.this);
		}
		ll_menus = (LinearLayout) findViewById(R.id.ll_menus);
		// nav_container = (FrameLayout) findViewById(R.id.nav_container);
		info_container = (FrameLayout) findViewById(R.id.info_container);
		LoadMenu();
		intentFilter.addAction(WifiP2pManager.WIFI_P2P_STATE_CHANGED_ACTION);
		intentFilter.addAction(WifiP2pManager.WIFI_P2P_PEERS_CHANGED_ACTION);
		intentFilter
				.addAction(WifiP2pManager.WIFI_P2P_CONNECTION_CHANGED_ACTION);
		intentFilter
				.addAction(WifiP2pManager.WIFI_P2P_THIS_DEVICE_CHANGED_ACTION);
		// 以下部分用于测试功能
		manager = (WifiP2pManager) getSystemService(Context.WIFI_P2P_SERVICE);
		// 暂时不用提示是否支持Wifi-Direct功能
		// if (manager == null) {
		// // Toast.makeText(MainActivity.this, "不支持wifi-direct功能",
		// // Toast.LENGTH_SHORT).show();
		// } else
		// channel = manager.initialize(this, getMainLooper(), null);
		// // nav_container = (FrameLayout)findViewById(R.id.nav_container);
		// // nav_container.setVisibility(View.GONE);//先隐藏掉 ，证明可以成功隐藏掉
		// if (manager != null && channel != null)// 开机就开起P2P服务
		// {
		// CommonUtil.enableP2p(MainActivity.this, manager, channel, null);
		// }
		// UpdateButtonState(false);
		UpdateOperAction();
		// InitBNData();
	}

	/**
	 * 销毁PopWindow的线程
	 */
	Handler dismissPopupWindowhandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			// 要做的事情
			if (popupWindow != null && popupWindow.isShowing())
				popupWindow.dismiss();
			if (task != null) {
				task.cancel();
			}
			super.handleMessage(msg);
		}
	};

	// 隐藏标题栏控件
	public void SetLayoutGone() {

		main_rl_1.setVisibility(View.GONE);
	}

	// 显示标题栏控件
	public void SetLayoutVisible() {

		main_rl_1.setVisibility(View.VISIBLE);
	}

	/**
	 * 加载菜单功能
	 */
	private void LoadMenu() {
		String strMenu = getSharedPreferences(Constants.MENU_INFO, 0)
				.getString(Constants.MENUDATA, "");
		Gson gson = new Gson();
		LinearLayout.LayoutParams vlp = new LinearLayout.LayoutParams(
				LinearLayout.LayoutParams.MATCH_PARENT, 60);
		vlp.topMargin = 5;
		if (strMenu.length() > 1) {
			try {
				int position = 0;
				lstMC = gson.fromJson(strMenu,
						new TypeToken<List<MenuClass>>() {
						}.getType());
				// int[] arrMC = new int[lstMC.size()];
				// 假设是有序的
				for (MenuClass mc : lstMC) {
					Button btn = new Button(this);
					btn.setLayoutParams(vlp);
					btn.setTextSize(14);
					btn.setId(mc.getMenuid());
					btn.setText(mc.getMenuname());
					btn.setBackgroundDrawable(getResources().getDrawable(
							R.drawable.menu_bg));
					// hmDP.put(ID_BUTTON_START, dp);
					btn.setOnClickListener(new View.OnClickListener() {

						@Override
						public void onClick(View v) {
							// TODO Auto-generated method stub
							// 直接在这里显示提示框

						}

					});
					ll_menus.addView(btn);
					hmControlsPosition.put(position, mc.getMenuid());
					position++;
				}
			} catch (Exception e) {
			}
		} else {
			int position = 0;
			MenuClass mc1 = new MenuClass();
			mc1.setMenuid(1);
			mc1.setOrdernum(1);
			mc1.setMenuname("HUD");
			mc1.setMenuicon(R.drawable.icon_alarm_clock);
			mc1.setFragmentname("");
			lstMC.add(mc1);
			MenuClass mc2 = new MenuClass();
			mc2.setMenuid(2);
			mc2.setOrdernum(2);
			mc2.setMenuname("电话");
			mc2.setMenuicon(R.drawable.icon_alarm_clock);
			mc2.setFragmentname("");
			lstMC.add(mc2);
			MenuClass mc3 = new MenuClass();
			mc3.setMenuid(3);
			mc3.setOrdernum(3);
			mc3.setMenuname("短信");
			mc3.setMenuicon(R.drawable.icon_alarm_clock);
			mc3.setFragmentname("");
			lstMC.add(mc3);
			MenuClass mc4 = new MenuClass();
			mc4.setMenuid(4);
			mc4.setOrdernum(4);
			mc4.setMenuname("OBD");
			mc4.setMenuicon(R.drawable.icon_alarm_clock);
			mc4.setFragmentname("");
			lstMC.add(mc4);
			String strMC = gson.toJson(lstMC);
			getSharedPreferences(Constants.MENU_INFO, 0).edit().putString(
					Constants.MENUDATA, strMC);
			// for (MenuClass mc : lstMC) {
			// Button btn = new Button(this);
			// btn.setLayoutParams(vlp);
			// btn.setTextSize(14);
			// btn.setId(mc.getMenuid());
			// btn.setText(mc.getMenuname());
			// btn.setBackgroundDrawable(getResources().getDrawable(
			// R.drawable.menu_bg));
			// // hmDP.put(ID_BUTTON_START, dp);
			// btn.setOnClickListener(new View.OnClickListener() {
			//
			// @Override
			// public void onClick(View v) {
			// // TODO Auto-generated method stub
			//
			// }
			// });
			// ll_menus.addView(btn);
			// hmControlsPosition.put(position, mc.getMenuid());
			// position++;
			// }
		}
	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		wifihot = new WifiApAdmin(this);
		registerReceiver(mNewMsgReceiver, makeNewMsgIntentFilter());
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		unregisterReceiver(mNewMsgReceiver);
	}

	class DismissPopupWindowTask extends TimerTask {
		@Override
		public void run() {
			// TODO Auto-generated method stub
			Message message = new Message();
			message.what = 1;
			dismissPopupWindowhandler.sendMessage(message);
		}
	}

	/**
	 * 显示新电话的提示框
	 */
	private void showCallPopupWindow(TelInfoClass tilu) {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(
				MainActivity.this).inflate(R.layout.popupwindow_newcall, null);
		// if (tilu.getTel_Photo() != null && tilu.getTel_Photo() != "") {
		// CircularImage ci_headphoto = (CircularImage) layout
		// .findViewById(R.id.ci_headphoto);
		// }
		if (tilu.getContects_Person() != null) {
			TextView tv_username = (TextView) layout
					.findViewById(R.id.tv_username);
			tv_username.setText(tilu.getContects_Person());
		}
		if (tilu.getContects_TelNum() != null) {
			TextView tv_phonenumber = (TextView) layout
					.findViewById(R.id.tv_phonenumber);
			tv_phonenumber.setText(tilu.getContects_TelNum());
		}
		popupWindow = new PopupWindow(MainActivity.this);
		popupWindow
				.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 3);
		popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(layout);
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_msgpopup_bg));
		// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);
		popupWindow.showAtLocation(findViewById(R.id.info_container),
				Gravity.CENTER_VERTICAL, 0, 0);// 需要指定Gravity，默认情况是center.
		task = new DismissPopupWindowTask();
		timer.schedule(task, 5000, 10000); // 4秒钟后销毁
		String tel_text = null;
		if (tilu.getContects_Person() != null
				&& !tilu.getContects_Person().equals("未知号码")) {
			tel_text = tilu.getContects_Person() + "来电话了,请选择接听或挂断";
		} else if (tilu.getContects_Person().equals("未知号码")
				&& tilu.getContects_TelNum() != null) {
			tel_text = tilu.getContects_Person() + tilu.getContects_TelNum()
					+ "来电话了,请选择接听或挂断";
		}
		if (tel_text != null) {
			svc.startSpeaking(tel_text);
		} else {
			return;
		}
	}

	/**
	 * 显示新消息的提示框
	 */
	private void showMsgPopupWindow(MsgInfoClass milu) {
		LinearLayout layout = (LinearLayout) LayoutInflater.from(
				MainActivity.this).inflate(R.layout.popupwindow_newmsg, null);
		// if (milu.getMsg_Photo() != null && milu.getMsg_Photo() != "") {
		// CircularImage ci_headphoto = (CircularImage) layout
		// .findViewById(R.id.ci_headphoto);
		// try {
		//
		// } catch (Exception e) {
		// }
		// }
		if (milu.getMsg_Content() != null) {
			TextView tv_msgcontent = (TextView) layout
					.findViewById(R.id.tv_msgcontent);
			tv_msgcontent.setText(milu.getMsg_Content());
		}
		if (milu.getMsg_DisplayName() != null) {
			TextView tv_username = (TextView) layout
					.findViewById(R.id.tv_username);
			tv_username.setText(milu.getMsg_DisplayName());
		}
		if (milu.getMsg_TelNum() != null) {
			TextView tv_phonenumber = (TextView) layout
					.findViewById(R.id.tv_phonenumber);
			tv_phonenumber.setText(milu.getMsg_TelNum());
		}

		popupWindow = new PopupWindow(MainActivity.this);
		popupWindow
				.setWidth(getWindowManager().getDefaultDisplay().getWidth() / 3);
		popupWindow.setHeight(LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.setFocusable(true);
		popupWindow.setContentView(layout);
		popupWindow.setBackgroundDrawable(getResources().getDrawable(
				R.drawable.ic_msgpopup_bg));
		// popupWindow.showAsDropDown(findViewById(R.id.tv_title), x, 10);
		popupWindow.showAtLocation(findViewById(R.id.info_container),
				Gravity.CENTER_VERTICAL, 0, 0);// 需要指定Gravity，默认情况是center.
		task = new DismissPopupWindowTask();
		timer.schedule(task, 5000, 2000); // 4秒钟后销毁

		String msg_test = null;
		if (milu.getMsg_DisplayName() != null) {
			msg_test = "您有新的消息，发送人，" + milu.getMsg_DisplayName() + ",是否朗读？";
		} else {
			msg_test = "未知号码" + milu.getMsg_TelNum() + "，发来短信，是否朗读？";
		}
		if (msg_test != null) {
			svc.startSpeaking(msg_test);
		}
	}

	// private void showTip(final String str){
	// runOnUiThread(new Runnable() {
	// @Override
	// public void run() {
	// mToast.setText(str);
	// mToast.show();
	// }
	// });
	// }

	/**
	 * 打开P2P服务，就创建群组
	 * 
	 * @param isWifiP2pEnabled
	 */
	// public void setIsWifiP2pEnabled(boolean isWifiP2pEnabled)// 打开与关闭与一体
	// {
	// Log.d(TAG, "isWifiP2pEnabled:" + isWifiP2pEnabled);
	// this.isWifiP2pEnabled = isWifiP2pEnabled;
	// if (isWifiP2pEnabled == true) {
	// manager.createGroup(channel, new ActionListener() {
	//
	// @Override
	// public void onSuccess() {
	// // TODO Auto-generated method stub
	// // Log.d(TAG, "createGroup:onSuccess");
	// Log.d("wifitest", "createGroup:onSuccess");
	// Intent intentService = new Intent(MainActivity.this,
	// ProcessMsgService.class);
	// MainActivity.this.stopService(intentService);
	// MainActivity.this.startService(intentService);
	// // CommonUtil.keepWiFiOn(MainActivity.this, true);
	// }
	//
	// @Override
	// public void onFailure(int reason) {
	// // TODO Auto-generated method stub
	// // Log.d(TAG, "createGroup:onFailure " + reason);
	// // Log.d("wifitest", " onFailure(int reason)"+reason);
	// // CommonUtil.disableP2p(MainActivity.this, manager,
	// // channel,
	// // null); // 关闭，在开启
	// // CommonUtil.enableP2p(MainActivity.this, manager, channel,
	// // null);
	// CommonUtil.RestartWiFi(MainActivity.this);
	// }
	// });
	// } else {
	// Intent intentService = new Intent(MainActivity.this,
	// ProcessMsgService.class);
	// MainActivity.this.stopService(intentService);
	// Log.d("wifitest", "MainActivity.this.stopService(intentService)");
	// if (manager != null && channel != null) {
	// CommonUtil.enableP2p(MainActivity.this, manager, channel, null);
	// Log.d("wifitest", "manager != null && channel != null");
	// }
	//
	// }
	// }

	@SuppressLint("NewApi")
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		// case R.id.iv_menu_map://弹出提示框
		// break;
		// case R.id.iv_menu_phone:
		// NewPhoneFragment pf = new NewPhoneFragment();
		// getFragmentManager().beginTransaction().replace(R.id.info_container,pf).commit();
		// break;
		// case R.id.iv_menu_msg:
		// NewMsgFragment nmf = new NewMsgFragment();
		// getFragmentManager().beginTransaction().replace(R.id.info_container,nmf).commit();
		// break;
		}
	}

	public void startNaviHud(StartNaviInfo info) {

		// mStartPoints.add(info.getNavi_start());
		// mEndPoints.add(info.getNavi_end());

		// mAMapNavi.setAMapNaviListener(MainActivity.this);

		// mAMapNavi.calculateDriveRoute(mStartPoints, mEndPoints, null,
		// AMapNavi.DrivingDefault);
		// Log.i(TAG, "mStartPoints：" + mStartPoints + "mEndPoints:" +
		// mEndPoints);
		// nav_container.setVisibility(View.VISIBLE);
		// info_container.setVisibility(View.GONE);
		// GeneralPathFragment gf = new GeneralPathFragment();
		Bundle bundle = new Bundle();
		bundle.putSerializable("navi_point_info", info);
		HudFragment hf = new HudFragment();
		hf.setArguments(bundle);
		getFragmentManager().beginTransaction()
		// .replace(R.id.nav_container, gf)
				.replace(R.id.info_container, hf).commit();
	}

	/**
	 * 广播接收器,需要对控件进行编号，然后按照顺序选择 ，先选择上下，在选择左右 ，比较复杂的手势操作，涉及到数据派发的问题
	 * 判断当前显示内容，在决定向哪里发送数据
	 */
	BroadcastReceiver mNewMsgReceiver = new BroadcastReceiver() {// 如果是地图页面的显示，就实现数据的分发。
		@Override
		public void onReceive(Context context, Intent intent) {
			// TODO Auto-generated method stub
			Log.i(TAG, "mNewMsgReceiver");
			Gson gson = new Gson();
			final String action = intent.getAction();
			Log.i(TAG, "mNewMsgReceiver" + action);
			if (action.equals("android.net.conn.CONNECTIVITY_CHANGE")) {
				if (!CommonUtil.isConnect(MainActivity.this)) {
					svc.startSpeaking("当前处于无网络状态，请检查！");
					main_rui_state
							.setBackgroundResource(R.drawable.test_sleeping);
				}
			}
			if (action.equals("onVolumeChanged")) {
				main_tv_back_music.setText(intent.getIntExtra(
						"onVolumeChanged", 0) + "");
			}
			// 首页导航
			if (action.equals(Constants.MAIN_NAVI_COM))// 如果是语音指令，就进入语音指令消息分发界面
			{
				Log.i(TAG, "MAIN_HOME_COM");
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_NAVI_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null
						&& svd.getValue().equals(Constants.CONTROL_MAIN_KEY)) {

					Bundle bundle = new Bundle();
					bundle.putString("destination", svd.getValue());
					GNaviFragment hf = new GNaviFragment();
					hf.setArguments(bundle);
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();

				} else if (svd.getValue() != null) {// 遥控是第二种判断
					VoiceDataClass vdc = gson.fromJson(svd.getValue(),
							VoiceDataClass.class);
					// 表明要继续导航
					if (vdc.getType().equals(Constants.F_R_CONTINUE_NAVI[0])) {

						HudFragment hf = new HudFragment();
						getFragmentManager().beginTransaction()
								.replace(R.id.info_container, hf).commit();

					} else if (vdc.getType().equals(Constants.F_R_NAV_GOTO[0])
							|| vdc.getType().equals(Constants.F_R_OPEN_NAVI[0])) {
						Bundle bundle = new Bundle();
						bundle.putString("destination", vdc.getValue());
						GNaviFragment hf = new GNaviFragment();
						hf.setArguments(bundle);
						getFragmentManager().beginTransaction()
								.replace(R.id.info_container, hf).commit();

					}
					// 路线全览
					else if (vdc.getType().equals(
							Constants.F_R_NAVI_ROUTE_PLAN_All_SEE[0])) {
						AllPlanSeeFragment hf = new AllPlanSeeFragment();
						getFragmentManager().beginTransaction()
								.replace(R.id.info_container, hf).commit();
					}
				}

				// hud显示界面
			} else if (action.equals(Constants.MAIN_HUD_COM)) {
				Log.i(TAG, "action.equals(Constants.MAIN_HUD_COM)");
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_HUD_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null) {
					sni = gson.fromJson(svd.getValue(), StartNaviInfo.class);
					Log.i(TAG, "startNaviHud(sni)");
					startNaviHud(sni);
				}
				// 结束导航
			} else if (action.equals(Constants.MAIN_RETURN_HOME_COM)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_RETURN_HOME_COM),
						SpeechVoiceData.class);
				if (svd.getValue().endsWith(Constants.MAIN_RETURN_HOME_COM)) {
					HomePJFragment hf = new HomePJFragment();
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();
				}
				// 打电话
			} else if (action.equals(Constants.MAIN_PHONE_COM)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_PHONE_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null) {
					Bundle bundle = new Bundle();
					bundle.putString("phoneInfos", svd.getValue());
					// nav_container.setVisibility(View.GONE);
					// info_container.setVisibility(View.VISIBLE);
					PhoneFragment hf = new PhoneFragment();
					hf.setArguments(bundle);
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();

				}
			}
			// 首页obd显示
			else if (action.equals(Constants.MAIN_OBD_COM)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_OBD_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null) {
					OBDFragment hf = new OBDFragment();
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();

				}
			}
			// 首页音乐功能（列表）
			else if (action.equals(Constants.MAIN_MUSIC_COM)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_MUSIC_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null) {
					MusicFragment hf = new MusicFragment();
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();

				}
			}
			// 音乐播放
			else if (action.equals(Constants.MAIN_MUSIC_PLAY_COM)) {
				SpeechVoiceData svd = gson.fromJson(
						intent.getStringExtra(Constants.MAIN_MUSIC_PLAY_COM),
						SpeechVoiceData.class);
				if (svd.getValue() != null) {
					Bundle bundle = new Bundle();
					// 说明是全部歌曲
					if (CommonUtil.isNumeric(svd.getValue())) {
						bundle.putString("choosedMusicId", svd.getValue());
						bundle.putString("listType", null);
					} else {
						MusicTypeClass mtc = gson.fromJson(svd.getValue(),
								MusicTypeClass.class);
						bundle.putString("choosedMusicId", mtc.getId());
						bundle.putString("listType", mtc.getMusicTypeList());
					}
					MusicPlayFragment hf = new MusicPlayFragment();
					hf.setArguments(bundle);
					getFragmentManager().beginTransaction()
							.replace(R.id.info_container, hf).commit();

				}
			}
			// 界面图标的显示与隐藏
			else if (action.equals(Constants.MAIN_ICON_STATE_ACTION)) {
				int iconType = intent.getIntExtra("iconType", 0);
				int iconState = intent.getIntExtra("iconState", 0);
				switch (iconType) {
				case Constants.MAIN_ICON_RUI_STATE:
					switch (iconState) {
					// 0:打招呼 1:听 2：思考 3：识别不出来 4：休息
					case 0:
						main_rui_state
								.setBackgroundResource(R.drawable.test_hello);
						break;
					case 1:
						main_rui_state
								.setBackgroundResource(R.drawable.test_listening);
						break;
					case 2:
						main_rui_state
								.setBackgroundResource(R.drawable.test_thinking);
						break;
					case 3:
						main_rui_state
								.setBackgroundResource(R.drawable.test_thinking_nothing);
						break;
					case 4:
						main_rui_state
								.setBackgroundResource(R.drawable.test_sleeping);
						break;

					default:
						break;
					}
					break;
				default:
					break;
				}
			}
			// 退出应用
			else if (action.equals(Constants.MAIN_EXIT_PROJECT_ACTION)) {
				MainActivity.this.finish();
				int id = android.os.Process.myPid();
				android.os.Process.killProcess(id);
			}
			// 每分钟更新一次
			else if (action.equals(Intent.ACTION_TIME_TICK)) {
				main_tv_time.setText(CommonUtil.timeShow());
			}
			// 电话提醒
			else if (action.equals(Constants.TELEPHONY_NEW)) {
				String strMsg = intent.getStringExtra(Constants.MSG_PARAM);
				Log.i(TAG, "有电话进来了" + strMsg);
				// 先将内容解析出来，再放到对应的数据中，再弹框处理
				TelInfoClass tilu = new TelInfoClass();
				try {
					tilu = gson.fromJson(strMsg, TelInfoClass.class);
					showCallPopupWindow(tilu);
				} catch (Exception e) {
					e.printStackTrace();
				}

			} else if (action.equals(Constants.SMS_NEW)) {// 短信来了
				MsgInfoClass milu = new MsgInfoClass();
				String strMsg = intent.getStringExtra(Constants.MSG_PARAM);
				Log.i(TAG, "有短信进来了" + strMsg);
				try {
					milu = gson.fromJson(strMsg, MsgInfoClass.class);
					// 先将内容解析出来，再放到对应的数据中，再弹框处理
					showMsgPopupWindow(milu);
				} catch (Exception e) {
				}

			} else if (Constants.MSG_NEW.equals(action)) {
				String strCmd = intent.getStringExtra(Constants.MSG_CMD);
				String strMsg = intent.getStringExtra(Constants.MSG_PARAM);
				Log.d(TAG, "strCmd:" + strCmd + "=+="
						+ Constants.MODEL_PHONE_ACTION);
				Log.d(TAG, "strMsg:" + strMsg);
				// 呼出电话信息回传
				if (Constants.MODEL_PHONE_ACTION.equals(strCmd)) {
					Log.i(TAG, TAG + "呼出电话信息回传");
					BroadCastNewMsg(Constants.MODEL_PHONE_ACTION, strMsg);

				}
				if (strCmd.startsWith("0x0")) {
					if (Constants.GESTURE_INFO.equals(strCmd)) {
						if (strMsg.equals(Constants.GESTURE_TOLEFT))// 向左
																	// 实现消息的分发,更新主菜单上的状态
						{
							if (OperFragmentFlag == 0) {
								OperFragmentFlag = 2;
								// 进入菜单栏的操作
							} else
								OperFragmentFlag = --OperFragmentFlag;
							switch (OperFragmentFlag) {
							case 0:
								// 判断当前的页面显示，如果nav没有显示，则直接跳过
								// if (nav_container.getVisibility() ==
								// View.GONE) {
								// OperFragmentFlag = 2;
								// // 将对应的按钮状态发生变化
								// UpdateButtonState(false);
								// } else {
								// Fragment f = getFragmentManager()
								// .findFragmentById(
								// R.id.nav_container);// 向上手势
								// // 修改为正确的显示
								// if (f instanceof NavFragment) {
								// UpdateButtonState(true);
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_TODOWN);
								// } else {
								// OperFragmentFlag = 2;
								// UpdateButtonState(false);
								// }
								// // 更新主菜单上Button的状态
								// }
								break;
							case 1:// 暂时先不管这个情况
									// 判断当前页面显示，如果info没有显示，则直接跳过
									// if (info_container.getVisibility() ==
									// View.GONE
								// && nav_container.getVisibility() ==
								// View.GONE) {
								// OperFragmentFlag = 2;
								// UpdateButtonState(false);
								// } else if (info_container.getVisibility() !=
								// View.GONE) {
								// OperFragmentFlag = 1;
								// // 想里面发送一个向上的操作
								// BroadCastNewMsg(Constants.MSG_MSG,
								// Constants.GESTURE_TOUP);
								// UpdateButtonState(true);
								// // 再将相应的按钮背景设置为未选择
								// } else {
								// OperFragmentFlag = 0;
								// // 想里面发送一个向上的操作
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_TOUP);
								// UpdateButtonState(true);
								// }
								// break;
							case 2:
								// UpdateButtonState(false);
								// String stringIntentNav = Constants.NAV_MSG;
								// Intent intentActionNav = new
								// Intent(stringIntentNav);
								// //
								// intentActionNav.putExtra(Constants.MSG_CMD,
								// newCmd);
								// intentActionNav.putExtra(Constants.MSG_PARAM,
								// Constants.GESTURE_TOUP);
								// MainActivity.this.sendBroadcast(intentActionNav);
								break;
							}
						} else if (strMsg.equals(Constants.GESTURE_TORIGHT))// 向右
						{
							OperFragmentFlag = ++OperFragmentFlag % 3;
							switch (OperFragmentFlag) {
							case 0:
								// 判断当前的页面显示，如果nav没有显示，则直接跳过
								// if (nav_container.getVisibility() ==
								// View.GONE
								// && info_container.getVisibility() ==
								// View.GONE) {
								// OperFragmentFlag = 2;
								// // 将对应的按钮状态发生变化
								// UpdateButtonState(false);
								// } else if (nav_container.getVisibility() !=
								// View.GONE) {
								// Fragment f = getFragmentManager()
								// .findFragmentById(
								// R.id.nav_container);// 向上手势
								// // 修改为正确的显示
								// if (f instanceof NavFragment) {
								// OperFragmentFlag = 0;
								// UpdateButtonState(true);
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_TODOWN);
								// } else {
								// OperFragmentFlag = 2;
								// UpdateButtonState(false);
								// }
								// } else if (info_container.getVisibility() !=
								// View.GONE) {
								// OperFragmentFlag = ++OperFragmentFlag % 3;
								// BroadCastNewMsg(Constants.MSG_MSG,
								// Constants.GESTURE_TODOWN);
								// UpdateButtonState(true);
								// }
								break;
							case 1:
								// 判断当前页面显示，如果info没有显示，则直接跳过
								if (info_container.getVisibility() == View.GONE) {
									OperFragmentFlag = ++OperFragmentFlag % 3;
									// UpdateButtonState(false);
								} else {
									// 依据类型进行判断
									// UpdateButtonState(true);
									BroadCastNewMsg(Constants.MSG_MSG,
											Constants.GESTURE_TODOWN);
								}
								break;
							case 2:
								// UpdateButtonState(false);
								// BroadCastNewMsg( Constants.NAV_MSG,
								// Constants.GESTURE_TOUP);
								break;
							}
						} else if (strMsg.equals(Constants.GESTURE_TOUP))// 向上,依据实际操作的页面进行数据派发
						{
							switch (OperFragmentFlag) {
							case 0:
								// Fragment f = getFragmentManager()
								// .findFragmentById(R.id.nav_container);// 向上手势
								// if (f instanceof NavFragment) {
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_TOUP);
								// }
								break;
							case 1:
								break;
							case 2:
								if (selectPosition > 0) {
									selectPosition = (selectPosition - 1)
											% lstMC.size();
									// UpdateButtonState(false);
								} else {
									selectPosition = 2;
									// UpdateButtonState(false);
								}
								break;
							}

						} else if (strMsg.equals(Constants.GESTURE_TODOWN))// 向下依据实际的页面进行数据派发
						{
							switch (OperFragmentFlag) {
							case 0:
								// Fragment f = getFragmentManager()
								// .findFragmentById(R.id.nav_container);// 向上手势
								// if (f instanceof NavFragment) {
								// // 向路径规划页面发送数据
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_TODOWN);
								// }
								break;
							case 1:
								break;
							case 2:
								selectPosition = ++selectPosition
										% lstMC.size();
								// UpdateButtonState(false);
								break;
							}

						} else if (strMsg.equals(Constants.GESTURE_SINGLETAP))// 单击
						{
							switch (OperFragmentFlag) {
							case 0:
								// Fragment f = getFragmentManager()
								// .findFragmentById(R.id.nav_container);// 向上手势
								// if (f instanceof NavFragment) {
								// // 向路径规划页面发送数据
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_SINGLETAP);
								// }
								break;
							case 1:
								break;
							case 2:
								UpdateOperAction();
								break;
							}

						} else if (strMsg.equals(Constants.GETSTURE_LONGPRESS))// 长按
						{
							switch (OperFragmentFlag) {
							case 0:
								// Fragment f = getFragmentManager()
								// .findFragmentById(R.id.nav_container);// 向上手势
								// if (f instanceof NavFragment) {
								// // 向路径规划页面发送数据
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GETSTURE_LONGPRESS);
								// }
								break;
							case 1:
								break;
							case 2:
								break;
							}

						} else if (strMsg.equals(Constants.GESTURE_DOUBLECLICK)) {
							switch (OperFragmentFlag) {
							case 0:
								// Fragment f = getFragmentManager()
								// .findFragmentById(R.id.nav_container);// 向上手势
								// if (f instanceof NavFragment) {
								// // 向路径规划页面发送数据
								// BroadCastNewMsg(Constants.NAV_MSG,
								// Constants.GESTURE_DOUBLECLICK);
								// }
								break;
							case 1:
								break;
							case 2:
								break;
							}
						} else if (strMsg.equals(Constants.OPER_BACK)) {
							switch (OperFragmentFlag) {
							case 0:
								break;
							case 1:
								break;
							case 2:
								break;
							}
						}
					} else if (Constants.SMS_NEW.equals(strCmd)) {// 短信来了
						MsgInfoClass milu = new MsgInfoClass();
						try {
							milu = gson.fromJson(strMsg, MsgInfoClass.class);
							// 先将内容解析出来，再放到对应的数据中，再弹框处理
							showMsgPopupWindow(milu);
						} catch (Exception e) {
						}

					} else if (Constants.TELEPHONY_NEW.equals(strCmd))// 如果有新电话来了，将使用弹框来做
					{
						Log.i(TAG, "有电话进来了" + strMsg);
						// 先将内容解析出来，再放到对应的数据中，再弹框处理
						TelInfoClass tilu = new TelInfoClass();
						try {
							tilu = gson.fromJson(strMsg, TelInfoClass.class);
							showCallPopupWindow(tilu);
						} catch (Exception e) {
						}

						// selectPosition = 1;
						// UpdateButtonState(false);
						// nav_container.setVisibility(View.GONE);
						// info_container.setVisibility(View.VISIBLE);
						// NewPhoneFragment pf = new NewPhoneFragment();
						// getFragmentManager().beginTransaction().replace(R.id.info_container,pf).commit();
					} else if (Constants.TELEPHONY_OFFHOOK.equals(strCmd))// 如果接听电话，改如何处理
					{
						// 无需任何操作
					} else if (Constants.TELEPHONY_IDLE.equals(strCmd))// 如果挂断电话，改如何处理
					{
						selectPosition = 0;
						// UpdateButtonState(false);
					} else if (Constants.MSG_CMD.equals(strCmd))// 如果有新消息来了，将使用弹框来做
					{

					} else if (Constants.OPER_NAV.equals(strCmd))// 如果是导航的命令，就进入导航的数据处理
					{
						// Log.d(TAG, "OPER_NAV:" + strMsg);
						// selectPosition = 0;
						// OperFragmentFlag = 0;
						// UpdateButtonState(true);
						// NavDataClass ndc = new NavDataClass();
						// Gson gson = new Gson();
						// try {
						// ndc = gson.fromJson(strMsg, NavDataClass.class);
						// if (ndc.getStartOrEndNav().equals("start")) {
						// NavFragment nf = NavFragment
						// .newInstance(strMsg);
						// nav_container.setVisibility(View.VISIBLE);
						// info_container.setVisibility(View.GONE);
						// getFragmentManager().beginTransaction()
						// .replace(R.id.nav_container, nf)
						// .commit();
						// } else {// 如果是结束导航，则直接使用nav界面来代替HUD界面
						// Toast.makeText(MainActivity.this, "结束导航!",
						// Toast.LENGTH_SHORT).show();
						// Fragment f = getFragmentManager()
						// .findFragmentById(R.id.nav_container);// 向上手势
						// if (f instanceof BNavigatorFragment) {
						// BaiduNaviManager.getInstance()
						// .destroyNMapView();
						// NavFragment nf = new NavFragment();//
						// 如果没有的话，就加入，如果有的话，就不用加入了。因为这个是唯一的？再考虑一下怎么做
						// getFragmentManager().beginTransaction()
						// .replace(R.id.nav_container, nf)
						// .commit();
						// }
						// // BNavigator.getInstance().onBackPressed();
						// //
						// BaiduNaviManager.getInstance().uninitEngine(MainActivity.this);
						// }
						// } catch (Exception e) {
						// }
						// BroadCastNewMsg(Constants.NAV_MSG,Constants.GESTURE_TODOWN);

					} else if (Constants.NAV_START.equals(strCmd))// 这个是开始导航，如果是结束导行，则怎么处理？
					{
						// Log.d(TAG, "nav param:" + strMsg);
						// // 先处理参数，再依据参数开始导航实现
						// LaunchNavParamClass lnpc = new LaunchNavParamClass();
						// try {
						// Gson gson = new Gson();
						// lnpc = gson.fromJson(strMsg,
						// LaunchNavParamClass.class);
						// } catch (Exception e) {
						// }
						// if (lnpc != null)// 如果是开始导航，如果是结束导行怎么处理
						// {
						// BaiduNaviManager
						// .getInstance()
						// .launchNavigator(
						// MainActivity.this,
						// lnpc.getStRouteNode().getLocation().latitude,
						// lnpc.getStRouteNode().getLocation().longitude,
						// lnpc.getStRouteNode().getTitle(),
						// lnpc.getEnRouteNode().getLocation().latitude,
						// lnpc.getEnRouteNode().getLocation().longitude,
						// lnpc.getEnRouteNode().getTitle(),
						// lnpc.getnERoutePlanMode(), //
						// 算路方式,对应的方式:ECAR_TIME_FIRST,ECAR_AVOID_JAM,ECAR_DIS_FIRST,ECAR_FEE_FIRST
						// true, // 真实导航
						// BaiduNaviManager.STRATEGY_FORCE_ONLINE_PRIORITY, //
						// 在离线策略
						// new OnStartNavigationListener() { // 跳转监听
						//
						// @Override
						// public void onJumpToNavigator(
						// Bundle configParams) { // 这个怎么处理问题
						// // 加载导航功能的页面
						// selectPosition = 0;
						// UpdateButtonState(false);
						// BNavigatorFragment bnf = BNavigatorFragment
						// .newInstance(configParams);
						// nav_container
						// .setVisibility(View.VISIBLE);
						// info_container
						// .setVisibility(View.GONE);
						// getFragmentManager()
						// .beginTransaction()
						// .replace(
						// R.id.nav_container,
						// bnf)
						// .commit();
						// // Intent intent = new
						// // Intent(MainActivity.this,
						// // BNavigatorActivity.class);//
						// // 进入了导航的界面
						// // intent.putExtras(configParams);
						// // startActivity(intent);
						// }
						//
						// @Override
						// public void onJumpToDownloader() {
						// }
						// });
						// }
					} else if (Constants.NAV_HUD_DATA.equals(strCmd))// hud分发消息
					{
						// Fragment f = getFragmentManager().findFragmentById(
						// R.id.nav_container);
						// if (f instanceof HudFragment) {
						// // 向HUD页面分发数据
						// BroadCastNewMsg(Constants.NAV_HUD_DATA, strMsg);
						// }

						// HudFragment nf = HudFragment
						// .newInstance(strMsg);
						// getFragmentManager().beginTransaction()
						// .replace(R.id.nav_container, nf)
						// .commit();
					} else if (Constants.RETURNWIFIDIRECTSTATE.equals(strCmd))// 表示连接成功
					{
						Toast.makeText(MainActivity.this, "wifi-Direct连接成功",
								Toast.LENGTH_LONG).show();
					}
				}
			}
		}
	};

	/**
	 * 广播过滤器
	 */
	private static IntentFilter makeNewMsgIntentFilter() {
		final IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(Constants.MSG_NEW);
		intentFilter.addAction(Constants.SMS_NEW);
		intentFilter.addAction(Constants.TELEPHONY_NEW);
		intentFilter.addAction(Constants.MAIN_HOME_COM);
		intentFilter.addAction(Constants.MAIN_HUD_COM);
		intentFilter.addAction(Constants.MAIN_NAVI_COM);
		intentFilter.addAction(Constants.MAIN_MUSIC_COM);// 音乐列表(即首页音乐)
		intentFilter.addAction(Constants.MAIN_MUSIC_PLAY_COM);// 音乐播放
		intentFilter.addAction(Constants.MAIN_PHONE_COM);
		intentFilter.addAction(Constants.MAIN_OBD_COM);
		intentFilter.addAction(Constants.MAIN_RETURN_HOME_COM);
		intentFilter.addAction(Constants.MAIN_EXIT_PROJECT_ACTION);
		intentFilter.addAction(Intent.ACTION_TIME_TICK);// 每分钟发送一次
		intentFilter.addAction(Constants.MAIN_ICON_STATE_ACTION);// 用于控制界面图标的显示、隐藏
		intentFilter.addAction("android.net.conn.CONNECTIVITY_CHANGE");// 用于控制界面图标的显示、隐藏
		intentFilter.addAction("onVolumeChanged");//

		return intentFilter;
	}

	/**
	 * 依据命令更新按钮的背景
	 * 
	 */
	// private void UpdateButtonState(boolean isClear) {
	// for (int i : hmControlsPosition.keySet()) {
	// findViewById(hmControlsPosition.get(i)).setBackgroundDrawable(
	// getResources().getDrawable(R.drawable.menu_normal));
	// }
	// if (!isClear)
	// findViewById(hmControlsPosition.get(selectPosition))
	// .setBackgroundDrawable(
	// getResources().getDrawable(R.drawable.menu_pressed));
	// }
	// /**
	// * 依据命令更新按钮的背景
	// *
	// */
	// private void UpdateButtonState(boolean isClear) {
	// for (int i : hmControlsPosition.keySet()) {
	// findViewById(hmControlsPosition.get(i)).setBackgroundDrawable(
	// getResources().getDrawable(R.drawable.menu_normal));
	// }
	// if (!isClear)
	// findViewById(hmControlsPosition.get(selectPosition))
	// .setBackgroundDrawable(
	// getResources().getDrawable(R.drawable.menu_pressed));
	// }

	/**
	 * 依据相应的命令更新操作
	 */
	private void UpdateOperAction() {
		switch (selectPosition) {
		case 0:
			// 判断，如果Nav存在，就直接显示Nav，如果是Hud存在，就直接显示HUD。
			// nav_container.setVisibility(View.VISIBLE);
			// info_container.setVisibility(View.GONE);
			// 再说吧
			// Fragment f = getFragmentManager().findFragmentById(
			// R.id.nav_container);// 向上手势
			// if (!(f instanceof HudFragment)) {
			// HudFragment nf = new HudFragment();//
			// 如果没有的话，就加入，如果有的话，就不用加入了。因为这个是唯一的？再考虑一下怎么做
			// getFragmentManager().beginTransaction()
			// .replace(R.id.nav_container, nf).commit();
			// }
			// nav_container.setVisibility(View.VISIBLE);
			// info_container.setVisibility(View.GONE);
			// AllPlanSeeFragment HomePJFragment
			Fragment f = getFragmentManager().findFragmentById(
					R.id.info_container);
			if (!(f instanceof HomePJFragment)) {
				HomePJFragment nf = new HomePJFragment();//
				// 如果没有的话，就加入，如果有的话，就不用加入了。因为这个是唯一的？再考虑一下怎么做
				getFragmentManager().beginTransaction()
						.replace(R.id.info_container, nf).commit();
			}
			break;
		case 1:
			// nav_container.setVisibility(View.GONE);
			// info_container.setVisibility(View.VISIBLE);
			NewPhoneFragment pf = new NewPhoneFragment();
			getFragmentManager().beginTransaction()
					.replace(R.id.info_container, pf).commit();
			break;
		case 2:
			// nav_container.setVisibility(View.GONE);
			// info_container.setVisibility(View.VISIBLE);
			NewMsgFragment nmf = new NewMsgFragment();
			getFragmentManager().beginTransaction()
					.replace(R.id.info_container, nmf).commit();
			break;
		}
	}

	// 初始化导航引擎
	private void InitBNData() {
		// 初始化导航引擎
		// BaiduNaviManager.getInstance().initEngine(this, getSdcardDir(),
		// mNaviEngineInitListener, ACCESS_KEY, mKeyVerifyListener);
		// BaiduNaviManager.getInstance().initEngine(MainActivity.this,
		// getSdcardDir(), mNaviEngineInitListener,
		// new LBSAuthManagerListener() {
		// @Override
		// public void onAuthResult(int status, String msg) {
		// String str = null;
		// if (0 == status) {
		// str = "key校验成功!";
		// } else {
		// str = "key校验失败, " + msg;
		// }
		// Toast.makeText(MainActivity.this, str,
		// Toast.LENGTH_LONG).show();
		// }
		// });
	}

	/**
	 * 发送新消息到本应用程序中
	 * 
	 * @param directionMsg
	 * @param param
	 */
	private void BroadCastNewMsg(String directionMsg, String param) {
		Intent intentActionNav = new Intent(directionMsg);
		intentActionNav.putExtra(Constants.MSG_PARAM, param);
		MainActivity.this.sendBroadcast(intentActionNav);
	}

	private String getSdcardDir() {
		if (Environment.getExternalStorageState().equalsIgnoreCase(
				Environment.MEDIA_MOUNTED)) {
			return Environment.getExternalStorageDirectory().toString();
		}
		return null;
	}

	@Override
	public void onStop() {
		super.onStop();
	}

	@Override
	public void onDestroy() {
		super.onDestroy();
		// 退出时释放连接
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// TODO Auto-generated method stub
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			// 先判断是否是Home，如果是，就不用返回为Home了
			Fragment f = getFragmentManager().findFragmentById(
					R.id.info_container);
			if (!(f instanceof HomePJFragment)) {
				HomePJFragment nf = new HomePJFragment();//
				// 如果没有的话，就加入，如果有的话，就不用加入了。因为这个是唯一的？再考虑一下怎么做
				getFragmentManager().beginTransaction()
						.replace(R.id.info_container, nf).commit();
			}
			return true;
		}
		return false;
	}

}
